package net.wantongtech.xa.uploadbasicinfo.service;

import cn.com.taiji.sdk.tools.JsonTools;
import com.google.common.io.Files;
import com.wtkj.zwxt.clearing.vo.clear.item.AbstractBaseClearItem;
import com.wtkj.zwxt.clearing.vo.clear.item.EtcClearItem;
import com.wtkj.zwxt.clearing.vo.clear.model.EtcClearModel;
import com.wtkj.zwxt.exceptions.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 恢复跨省争议
 *
 * @author MaShibo
 * 日期 :  2020/3/9
 */
@Service
@Slf4j
public class RecoveryOutDisputedTransService {

    @SuppressWarnings("UnstableApiUsage")
    private void recovery() {
        File directory = new File("C:\\Users\\MaShibo\\Desktop\\20200229省内清分文件");
        File[] jsonFiles = directory.listFiles((dir, name) -> name.endsWith(".json"));
        if (jsonFiles == null || jsonFiles.length == 0) {
            log.info("文件夹{}中，不存在json文件", directory);
            return;
        }
        List<EtcClearModel> modelList = Arrays.stream(jsonFiles)
                .map(jsonFile -> {
                    try {
                        String json = Files.toString(jsonFile, Charset.forName("UTF-8"));
                        return JsonTools.json2Object(json, EtcClearModel.class);
                    } catch (IOException e) {
                        log.error(e.getMessage(), e);
                        throw new BusinessException("解析json文件失败");
                    }
                }).collect(Collectors.toList());
        Map<Long, EtcClearItem> clearItemMap = modelList.stream()
                .flatMap(etcClearModel -> etcClearModel.getClearDetail().stream())
                .flatMap(clearDetail -> clearDetail.getClearItems().stream())
                .collect(Collectors.toMap(AbstractBaseClearItem::getFileId, v -> v));
        log.info("clearItemMap：{}", clearItemMap);
        Set<Long> fileIdSet = clearItemMap.keySet();
        log.info("fileIdSet：{}", fileIdSet);

    }

    public static void main(String[] args) {
        new RecoveryOutDisputedTransService().recovery();
    }
}
